home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / Libraries / Activation / Focus.cp < prev    next >
Text File  |  1997-06-28  |  2KB  |  119 lines

  1. // Focus.cp
  2.  
  3. #ifndef Focus_h
  4. #include "Focus.h"
  5. #endif
  6. #ifndef ListLoop_h
  7. #include "ListLoop.h"
  8. #endif
  9. #ifndef Activator_h
  10. #include "Activator.h"
  11. #endif
  12.  
  13. Focus::Focus()
  14.   : parent( 0 ),
  15.      favoredChild( 0 ),
  16.      children( 0 ),
  17.      active( false )
  18.   {
  19.   }
  20.  
  21. Focus::Focus( Below, Focus& theParent )
  22.   : parent( &theParent ),
  23.      favoredChild( 0 ),
  24.      children( 0 ),
  25.      active( false )
  26.   {
  27.     Assert( theParent.children < maxuint32 );
  28.     theParent.children++;
  29.     if ( theParent.favoredChild == 0 && !theParent.active )
  30.         theParent.favoredChild = this;
  31.   }
  32.  
  33. Focus::~Focus()
  34.   {
  35.     Assert( !active );
  36.     Assert( children == 0 );
  37.     Assert( favoredChild == 0 );
  38.     
  39.     if ( parent != 0 )
  40.       {
  41.         Assert( parent->children > 0 );
  42.         parent->children--;
  43.         if ( parent->favoredChild == this )
  44.             parent->favoredChild = 0;
  45.       }
  46.   }
  47.  
  48. void Focus::FavorNoChild()
  49.   {
  50.     if ( favoredChild == 0 )
  51.         return;
  52.     
  53.     if ( active )
  54.         favoredChild->Deactivate();
  55.     
  56.     favoredChild = 0;
  57.   }
  58.  
  59. void Focus::FavorChild( Focus& newFavorite )
  60.   {
  61.     Assert( newFavorite.parent == this );
  62.     if ( favoredChild == &newFavorite )
  63.         return;
  64.     
  65.     if ( active && favoredChild != 0 )
  66.         favoredChild->Deactivate();
  67.     
  68.     favoredChild = &newFavorite;
  69.     
  70.     if ( active )
  71.         newFavorite.Activate();
  72.   }
  73.  
  74. void Focus::Activate()
  75.   {
  76.     Assert( !active );
  77.     Assert( parent == 0 || parent->active );
  78.     Assert( parent == 0 || parent->favoredChild == this );
  79.     
  80.     active = true;
  81.     
  82.     for ( ListLoop<Activator> a( activators ); a.Unfinished(); a++ )
  83.         a->FocusActivated();
  84.     
  85.     if ( favoredChild != 0 )
  86.         favoredChild->Activate();
  87.   }
  88.  
  89. void Focus::Deactivate()
  90.   {
  91.     Assert( active );
  92.     Assert( parent == 0 || parent->active );
  93.     Assert( parent == 0 || parent->favoredChild == this );
  94.  
  95.     if ( favoredChild != 0 )
  96.         favoredChild->Deactivate();
  97.  
  98.     active = false;
  99.  
  100.     for ( ListLoop<Activator> a( activators, atEnd ); a.Unfinished(); a-- )
  101.         a->FocusDeactivated();
  102.   }
  103.  
  104. void Focus::BeFavored()
  105.   {
  106.     Assert( parent != 0 );
  107.     parent->FavorChild( *this );
  108.   }
  109.  
  110. void Focus::BeDisfavored()
  111.   {
  112.     Assert( parent != 0 );
  113.     if ( parent->FavoredChild() == this )
  114.         parent->FavorNoChild();
  115.   }
  116.  
  117. #include "ListOf.cp"
  118. #include "ListLoop.cp"
  119.